{include file="common/_header"}
<link rel="stylesheet" href="/static/X-admin-v2.2/X-admin/css/login.css">
<body class="login-bg">
<div class="login layui-anim layui-anim-up">
    <div class="message">CMS注册</div>
    <div id="darkbannerwrap"></div>

    <form class="layui-form" >
        <input type="hidden" name="__token__" id="__token__"  value="{$Request.token}" />
        <input type="text" id="username" placeholder="用户名" name="username" required="" lay-verify="username" autocomplete="off" class="layui-input">
        <hr class="hr15">
        <input type="text" id="nickname" placeholder="昵称" name="nickname" required="" lay-verify="nickname" autocomplete="off" class="layui-input">
        <hr class="hr15">
        <input type="password" id="password" placeholder="密码" name="password" required="" lay-verify="password" autocomplete="off" class="layui-input">
        <hr class="hr15">
        <input type="password" id="repass" placeholder="确认密码" name="repass" required="" lay-verify="repass" autocomplete="off" class="layui-input">
        <hr class="hr15">
        <input value="注册" lay-submit="" lay-filter="register" style="width:100%;" type="button">
        <hr class="hr20" >
    </form>
    <a href="login.html">
        <input value="登入" style="width:100%;text-align: center;background-color: #189F92;color: white;font-size: 18px" type="button">
    </a>
</div>

<script>
    $(function  () {
        layui.use(['form', 'layer'],
            function() {
                $ = layui.jquery;
                var form = layui.form,
                    layer = layui.layer;

                //自定义验证规则
                form.verify({
                    username : function(value) {
                        if (value.length < 5) {
                            return '昵称至少得5个字符啊';
                        }
                    },
                    nickname: function(value) {
                        if (value.length < 2) {
                            return '昵称至少得2个字符啊';
                        }
                    },
                    password : [/(.+){6,12}$/, '密码必须6到12位'],
                    repass : function(value) {
                        if ($('#password').val() != $('#repass').val()) {
                            return '两次密码不一致';
                        }
                    }
                });


                //监听提交
                form.on('submit(register)', function(data) {
                        //发异步，把数据提交给php
                    $.ajax({
                        url : "{:url('registerUser')}",
                        type : "POST",
                        data : data.field,
                        dataType : "JSON",
                        async : false,
                        success (res) {
                            if (res.code === 1) {
                                layer.open({
                                    title : res.msg,
                                    content : res.content,
                                    btn : ['确认'],
                                    yes : function () {
                                        window.location.href = "{:url('admin/user/login')}";
                                    }
                                });
                            } else {
                                layer.open({
                                    title : res.msg,
                                    content : res.content
                                })
                                $("#__token__").val(res.token);
                            }
                        }
                    });
                    return false;
                });
            });
        });
</script>
</body>
</html>